﻿Public Class perRegiao
    Inherits perPadrao
#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Propriedades"

    Public ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String

            sSql = "SELECT IDRegiao, Regiao"
            sSql &= " FROM DC_Regioes"
            sSql &= " ORDER BY Regiao"

            Return sSql
        End Get
    End Property

#End Region

#Region "Métodos"

    Public Function inserirRegiao(ByVal sRegiao As String, _
                                  ByVal iIdDiaAtendimento As Int32) As Int32

        Dim iIdRegiao As Int32
        Dim sSql As String

        Try
            iIdRegiao = Me.objBuscaId.BuscaID("IDRegiao", "DC_Regioes")

            sSql = "  INSERT INTO DC_Regioes "
            sSql &= " ( "
            sSql &= "   IDRegiao, "
            sSql &= "   Regiao, "
            sSql &= "   IDDiaAtendimento "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= " ( "
            sSql &= iIdRegiao & ", "
            sSql &= "'" & sRegiao & "',"
            sSql &= iIdDiaAtendimento
            sSql &= " ) "

            MyBase.executarAcao(sSql)
            Return iIdRegiao
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirRegiao'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarRegiao(ByVal iIdRegiao As Int32, _
                             ByVal sRegiao As String, _
                             ByVal iIdDiaAtendimento As Int32)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_Regioes SET "
            sSql &= "   Regiao = '" & sRegiao & "',"
            sSql &= "   IDDiaAtendimento = " & iIdDiaAtendimento
            sSql &= " WHERE "
            sSql &= "   IdRegiao = " & iIdRegiao

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarRegiao'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirRegiao(ByVal iIdRegiao As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Regioes "
            sSql &= " WHERE "
            sSql &= "   IDRegiao = " & iIdRegiao

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirRegiao'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoRegiao(ByVal sCampo As String, _
                                          ByVal iIDRegiao As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_Regioes "

            If (iIDRegiao > 0) Then
                sSql &= " WHERE IDRegiao = " & iIDRegiao
            End If

            dsDados = MyBase.executarConsulta(sSql, "DC_Regioes")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Regioes").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Regioes")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoRegiao'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarRegiao(ByVal iIdRegiao As Int32) As DataRow
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Regioes "
            sSql &= " WHERE IDRegiao = " & iIdRegiao

            dsDados = MyBase.executarConsulta(sSql, "DC_Regioes")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Regioes").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Regioes").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarRegiao'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
#End Region
End Class
